package com.songtai.action.front;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.songtai.action.SongtaiActionSupport;
import com.songtai.po.Node;
import com.songtai.po.PhotoObj;
import com.songtai.pojo.NodeView;
import com.songtai.service.ImageService;
import com.songtai.service.NodeService;
import com.songtai.util.CommenUtil;
import com.songtai.util.Constants;
import com.songtai.util.Pager;

/**
 * @author 赵中杨
 * @email zhaozhongyang717@gmail.com
 * @date 2010 2010-4-22 上午11:35:31
 */
public class ProductAction extends SongtaiActionSupport {

	private static final long serialVersionUID = 1L;
	private Long nodeid;
	private ImageService imageService;
	private NodeService nodeService;
	private int parent;
	private int currentPage;
	private Pager<PhotoObj> products;
	private List<NodeView> nodes;
	private Node currentNode;
	private List<PhotoObj> topPro;
	private String keyword;
	private String pid;
	private PhotoObj product;

	/**
	 * 获取成功案例(含搜索)
	 * 
	 * @return
	 * @throws Exception
	 */
	public String cases() throws Exception {
		if (nodeid<=0) {
			return null;
		}
		if (currentPage < 1) {
			currentPage = 1;
		}
		if (parent > 0) {
			nodes = nodeService.getChildren(nodeid);
			if (nodes != null && !nodes.isEmpty()) {
				Long nid = nodes.get(0).getId();
				currentNode = nodeService.getNodeById(nid);
				if (CommenUtil.isEmpty(keyword)) {
					products = imageService.getAllImage(currentPage, Constants.DEFAULT_PAGESIEZE, Constants.IMAGE_CASE, null);
				} else {
					products = imageService.getAllImage(keyword, currentPage, Constants.DEFAULT_PAGESIEZE, Constants.IMAGE_CASE);
				}
				//topPro = imageService.getRecentImage(100, Constants.IMAGE_CASE);
				topPro = copyList(topPro, 3);
			}

		} else {
			nodes = nodeService.getAllBrothers(nodeid);
			currentNode = nodeService.getNodeById(nodeid);
			if (CommenUtil.isEmpty(keyword)) {
				products = imageService.getAllImage(currentPage, Constants.DEFAULT_PAGESIEZE, Constants.IMAGE_CASE, nodeid);
			} else {
				products = imageService.getAllImage(keyword, currentPage, Constants.DEFAULT_PAGESIEZE, Constants.IMAGE_CASE);
			}
			//topPro = imageService.getRecentImage(100, Constants.IMAGE_CASE);
			topPro = copyList(topPro, 3);
		}
		return SUCCESS;
	}

	/**
	 * 获取产品(含搜索)
	 * 
	 * @return
	 * @throws Exception
	 */
	public String products() throws Exception {
		if (nodeid<1) {
			return null;
		}
		if (currentPage < 1) {
			currentPage = 1;
		}
		if (parent > 0) {
			nodes = nodeService.getChildren(nodeid);
			if (nodes != null && !nodes.isEmpty()) {
				Long nid = nodes.get(0).getId();
				currentNode = nodeService.getNodeById(nid);
				if (CommenUtil.isEmpty(keyword)) {
					products = imageService.getAllImage(currentPage, Constants.DEFAULT_PAGESIEZE, Constants.IMAGE_PRODUCT, null);
				} else {
					products = imageService.getAllImage(keyword, currentPage, Constants.DEFAULT_PAGESIEZE, Constants.IMAGE_PRODUCT);
				}
				//topPro = imageService.getRecentImage(100, Constants.IMAGE_PRODUCT);
				topPro = copyList(topPro, 3);
			}

		} else {
			nodes = nodeService.getAllBrothers(nodeid);
			currentNode = nodeService.getNodeById(nodeid);
			if (CommenUtil.isEmpty(keyword)) {
				products = imageService.getAllImage(currentPage, Constants.DEFAULT_PAGESIEZE, Constants.IMAGE_PRODUCT, nodeid);
			} else {
				products = imageService.getAllImage(keyword, currentPage, Constants.DEFAULT_PAGESIEZE, Constants.IMAGE_PRODUCT);
			}
			//topPro = imageService.getRecentImage(100, Constants.IMAGE_PRODUCT);
			topPro = copyList(topPro, 3);
		}
		return SUCCESS;
	}

	/**
	 * 产品详情
	 * 
	 * @return
	 * @throws Exception
	 */
	public String detail_p() throws Exception {
		currentNode = nodeService.getNodeById(nodeid);
		product = imageService.getImageById(pid);
		return SUCCESS;
	}

	/**
	 * 案例详情
	 * 
	 * @return
	 * @throws Exception
	 */
	public String detail_c() throws Exception {
		currentNode = nodeService.getNodeById(nodeid);
		product = imageService.getImageById(pid);
		return SUCCESS;
	}

	public List<PhotoObj> copyList(List<PhotoObj> src, int length) {
		if (src == null || src.isEmpty()) {
			return null;
		}
		if (length > src.size()) {
			length = src.size();
		}
		List<PhotoObj> dest = new ArrayList<PhotoObj>();
		Collections.shuffle(src);
		for (int i = 0; i < length; i++) {
			dest.add(src.get(i));
		}
		return dest;
	}

	/**
	 * @return the nodeid
	 */
	public Long getNodeid() {
		return nodeid;
	}

	/**
	 * @param nodeid the nodeid to set
	 */
	public void setNodeid(Long nodeid) {
		this.nodeid = nodeid;
	}

	/**
	 * @return the imageService
	 */
	public ImageService getImageService() {
		return imageService;
	}

	/**
	 * @param imageService
	 *            the imageService to set
	 */
	public void setImageService(ImageService imageService) {
		this.imageService = imageService;
	}

	/**
	 * @return the nodeService
	 */
	public NodeService getNodeService() {
		return nodeService;
	}

	/**
	 * @param nodeService
	 *            the nodeService to set
	 */
	public void setNodeService(NodeService nodeService) {
		this.nodeService = nodeService;
	}

	/**
	 * @return the parent
	 */
	public int getParent() {
		return parent;
	}

	/**
	 * @param parent
	 *            the parent to set
	 */
	public void setParent(int parent) {
		this.parent = parent;
	}

	

	/**
	 * @return the nodes
	 */
	public List<NodeView> getNodes() {
		return nodes;
	}

	/**
	 * @param nodes
	 *            the nodes to set
	 */
	public void setNodes(List<NodeView> nodes) {
		this.nodes = nodes;
	}

	/**
	 * @return the currentNode
	 */
	public Node getCurrentNode() {
		return currentNode;
	}

	/**
	 * @param currentNode
	 *            the currentNode to set
	 */
	public void setCurrentNode(Node currentNode) {
		this.currentNode = currentNode;
	}

	/**
	 * @return the currentPage
	 */
	public int getCurrentPage() {
		return currentPage;
	}

	/**
	 * @param currentPage
	 *            the currentPage to set
	 */
	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	
	/**
	 * @return the keyword
	 */
	public String getKeyword() {
		return keyword;
	}

	/**
	 * @param keyword
	 *            the keyword to set
	 */
	public void setKeyword(String keyword) {
		this.keyword = keyword;
	}

	/**
	 * @return the pid
	 */
	public String getPid() {
		return pid;
	}

	/**
	 * @param pid
	 *            the pid to set
	 */
	public void setPid(String pid) {
		this.pid = pid;
	}

	/**
	 * @return the products
	 */
	public Pager<PhotoObj> getImages() {
		return products;
	}

	/**
	 * @param products the products to set
	 */
	public void setImages(Pager<PhotoObj> products) {
		this.products = products;
	}

	/**
	 * @return the topPro
	 */
	public List<PhotoObj> getTopPro() {
		return topPro;
	}

	/**
	 * @param topPro the topPro to set
	 */
	public void setTopPro(List<PhotoObj> topPro) {
		this.topPro = topPro;
	}

	/**
	 * @return the product
	 */
	public PhotoObj getImage() {
		return product;
	}

	/**
	 * @param product the product to set
	 */
	public void setImage(PhotoObj product) {
		this.product = product;
	}

	

}
